package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.vo.scene.GroupProdAttrModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ProdRoleModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ProductModuleVO;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.PostponeInfoModuleVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.cache.ProdAttrCache;
import com.iwhalecloud.bss.kite.dataservice.cache.ProdRelCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.ProdAttr;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.ProdRelInfo;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 延期信息
 *
 * @author zou.qinghua
 */
@Service
public class PostponeInfoModuleHandler extends AbstractModuleHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        IOperateSceneInstService operateSceneInstService = SpringUtil.getBean(IOperateSceneInstService.class);
        SceneNewOfferSeneVO sceneNewOfferSeneVO = (SceneNewOfferSeneVO) context.getSceneVo();
        String custId = context.getAcceptRequest().getCustId();
        String moduleId = sceneModule.getKiteModule().getModuleId();
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        String aPostponeProdId = DcPublicCache.getPCode("20210326001","postponeProdId");
        String zpostponeProdId = DcPublicCache.getCodea("20210326001","postponeProdId");
        PostponeInfoModuleVO postponeInfoModuleVO = new PostponeInfoModuleVO();
        postponeInfoModuleVO.setIsEdit("T");
        postponeInfoModuleVO.setIsShow("T");
        postponeInfoModuleVO.setOrderId(sceneModule.getKiteModule().getOrderId());
        postponeInfoModuleVO.setLayoutPage(sceneModule.getKiteModule().getLayoutPage());
        postponeInfoModuleVO.setModuleName(sceneModule.getKiteModule().getModuleName());
        KiteAttr relOffer = querySceneInstService.getCommonAttrsByFieldName(sceneInstId, "relOffers", moduleId);
        if(KiteObjectUtils.isEmpty(relOffer)){
            return Collections.singletonList(postponeInfoModuleVO);
        }
        String accNum = querySceneInstService.getCommonAttrValue(sceneInstId, "business_number");
        List<KiteAttrValue> attrValues = new ArrayList<>();

        List<ProductModuleVO> products = sceneNewOfferSeneVO.getSceneProductModuleVO().getProduct();
        for (ProductModuleVO product : products) {
            product.setIsShow("F");
        }
        List<ProductModuleVO> copyProducts = KiteObjectUtils.clone(products);
        String changeValue = "";
        for (ProductModuleVO copyProduct : copyProducts) {
            String prodName = copyProduct.getProdName();
            String prodId = copyProduct.getProdId();
            List<ProdRoleModuleVO> roles = copyProduct.getProdRole();
            for (ProdRoleModuleVO role : roles) {
                String roleProdId = role.getProdId();
                String prodInstId = role.getProdInstId();
                List<GroupProdAttrModuleVO> groupProdAttrs = role.getGroupProdAttr();
                boolean hasPostponeInfo = false;
                for (GroupProdAttrModuleVO groupProdAttr : groupProdAttrs) {
                    String groupId = groupProdAttr.getGroupId();
                    if (KiteStringUtils.isEqual(aPostponeProdId, groupId)) {
                        hasPostponeInfo = true;
                    }
                }
                if (KiteStringUtils.isEqual(roleProdId, prodId) && hasPostponeInfo) {
                    List<ProdRelInfo> prodRelInfos = ProdRelCache.get(roleProdId);
                    if(KiteListUtils.isNotEmpty(prodRelInfos)){
                        ProdRelInfo prodRelInfo = prodRelInfos.get(0);
                        zpostponeProdId = prodRelInfo.getzProdId();
                    }
                    KiteAttrValue attrValue = new KiteAttrValue();
                    attrValue.setAttrValue(prodInstId);
                    attrValue.setAttrValueDesc(prodName);
                    attrValues.add(attrValue);
                    List<KiteProdInstFunc> prodInstFuncs = querySceneInstService.listProdInstFuncsByProdId(custId,prodInstId,zpostponeProdId);
                    if(KiteListUtils.isNotEmpty(prodInstFuncs)){
                        changeValue += ","+prodInstId;
                    }
                }
            }
        }
        if(!KiteStringUtils.isEmpty(changeValue)){
            relOffer.setValue(changeValue);
            SpringUtil.getBean(IOperateSceneCommonService.class).setCommonAttr(KiteWebContext.getContext().getCurrentSceneInstId(), relOffer);
        }
        if(!KiteObjectUtils.isEmpty(relOffer)){
            String value = relOffer.getValue();
            if(!KiteStringUtils.isEmpty(value)){
                String[] ids = value.split(",");
                if (!KiteObjectUtils.isEmpty(ids)) {
                    Map<String, Map<String, List<KiteAttr>>> postponeInfo = new HashMap<>();
                    for (String id : ids) {
                        if (KiteStringUtils.isEmpty(id)) {
                            continue;
                        }
                        List<KiteProdInstFunc> funcs = querySceneInstService.listProdInstFuncsByProdId(custId, id, zpostponeProdId);
                        if (KiteListUtils.isEmpty(funcs)) {
                            continue;
                        }
                        Map<String, List<KiteAttr>> funcAttrs = new HashMap<>();
                        for (KiteProdInstFunc func : funcs) {
                            List<KiteAttr> attrs = func.listAttrs();
                            attrs = attrs.stream().filter(attr -> !KiteStringUtils.equals("E",attr.getShowType())).collect(Collectors.toList());
                            List<ProdAttr> prodAttrList = ProdAttrCache.get(func.getSpecId());
                            for (KiteAttr attr : attrs) {
                                if (ActionType.K.equals(func.getActionType())) {
                                    attr.setIsEdit("F");
                                }
                                if (KiteListUtils.isNotEmpty(prodAttrList)) {
                                    for (ProdAttr prodAttr : prodAttrList) {
                                        if (prodAttr.getFieldName().equals(attr.getFieldName())) {
                                            //还原表单类型
                                            attr.setAttrValueTypeId(prodAttr.getAttrValueTypeId());
                                            //ActionType为A时 还原表单是否可编辑
                                            if (ActionType.A.equals(func.getActionType())) {
                                                attr.setIsEdit(prodAttr.getIsEdit());
                                            }
                                            break;
                                        }
                                    }
                                }
                                if (KiteStringUtils.equals("200002981", attr.getFieldName())) {
                                    attr.setValue(accNum);
                                }
                                operateSceneInstService
                                    .setAttr(sceneInstId, func.getInstType(), func.getProdInstId(), attr);
                            }
                            funcAttrs.put(func.getProdInstId(), attrs);
                        }
                        postponeInfo.put(id, funcAttrs);
                    }
                    postponeInfoModuleVO.setPostponeInfo(postponeInfo);
                }
            }
        }
        postponeInfoModuleVO.setPostponeProdId(zpostponeProdId);
        if (KiteListUtils.isNotEmpty(attrValues)) {
            relOffer.setAttrValues(attrValues);
            SpringUtil.getBean(IOperateSceneCommonService.class).setCommonAttr(sceneInstId, relOffer);
            List<KiteAttr> relOffers = new ArrayList<>();
            relOffers.add(relOffer);
            postponeInfoModuleVO.setRelOffers(relOffers);
            sceneNewOfferSeneVO.getSceneProductModuleVO().setPostponeInfoModuleVO(postponeInfoModuleVO);
        }
        return Collections.singletonList(postponeInfoModuleVO);
    }

    @Override
    public List<? extends ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        String custId = context.getAcceptRequest().getCustId();
        String moduleId = sceneModule.getKiteModule().getModuleId();
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        String postponeProdId = DcPublicCache.getCodea("20210326001","postponeProdId");
        PostponeInfoModuleVO postponeInfoModuleVO = new PostponeInfoModuleVO();
        KiteAttr relOffer = querySceneInstService.getCommonAttrsByFieldName(sceneInstId, "relOffers", moduleId);
        SceneNewOfferSeneVO sceneNewOfferSeneVO = (SceneNewOfferSeneVO) context.getSceneVo();
        List<ProductModuleVO> products = sceneNewOfferSeneVO.getSceneProductModuleVO().getProduct();
        for (ProductModuleVO product : products) {
            product.setIsShow("F");
        }
        if(!KiteObjectUtils.isEmpty(relOffer)){
            String value = relOffer.getValue();
            if(!KiteStringUtils.isEmpty(value)){
                String[] ids = value.split(",");

                if (!KiteObjectUtils.isEmpty(ids)) {
                    Map<String, Map<String, List<KiteAttr>>> postponeInfo = new HashMap<>();
                    for (String id : ids) {
                        if (KiteStringUtils.isEmpty(id)) {
                            continue;
                        }
                        KiteProdInst kiteProdInst = querySceneInstService.getProdInst(custId, id);
                        String prodId = kiteProdInst.getProdId();
                        List<ProdRelInfo> prodRelInfos = ProdRelCache.get(prodId);
                        if(KiteListUtils.isNotEmpty(prodRelInfos)){
                            ProdRelInfo prodRelInfo = prodRelInfos.get(0);
                            postponeProdId = prodRelInfo.getzProdId();
                        }
                        List<KiteProdInstFunc> funcs = querySceneInstService.listProdInstFuncsByProdId(custId, id, postponeProdId);
                        if (KiteListUtils.isEmpty(funcs)) {
                            continue;
                        }
                        Map<String, List<KiteAttr>> funcAttrs = new HashMap<>();
                        IOperateSceneInstService operateSceneInstService = SpringUtil
                            .getBean(IOperateSceneInstService.class);
                        for (KiteProdInstFunc func : funcs) {
                            List<KiteAttr> attrs = func.listAttrs();
                            attrs = attrs.stream().filter(attr -> !KiteStringUtils.equals("E", attr.getShowType()))
                                .collect(Collectors.toList());
                            for (KiteAttr attr : attrs) {
                                attr.setIsEdit("F");
                                attr.setAttrValueTypeId("TT");
                                operateSceneInstService
                                    .setAttr(sceneInstId, func.getInstType(), func.getProdInstId(), attr);
                            }
                            funcAttrs.put(func.getProdInstId(), attrs);
                        }
                        postponeInfo.put(id, funcAttrs);
                    }
                    List<KiteAttr> relOffers = new ArrayList<>();
                    relOffer.setIsEdit("F");
                    relOffers.add(relOffer);
                    postponeInfoModuleVO.setRelOffers(relOffers);
                    postponeInfoModuleVO.setIsEdit("F");
                    postponeInfoModuleVO.setIsShow("T");
                    postponeInfoModuleVO.setLayoutPage(sceneModule.getKiteModule().getLayoutPage());
                    postponeInfoModuleVO.setModuleName(sceneModule.getKiteModule().getModuleName());
                    postponeInfoModuleVO.setOrderId(sceneModule.getKiteModule().getOrderId());
                    postponeInfoModuleVO.setPostponeInfo(postponeInfo);
                    postponeInfoModuleVO.setPostponeProdId(postponeProdId);
                    sceneNewOfferSeneVO.getSceneProductModuleVO().setPostponeInfoModuleVO(postponeInfoModuleVO);
                }
            }
        }
        return Collections.singletonList(postponeInfoModuleVO);
    }
}
